package com.shop.controller;

import com.shop.annotation.RequireRoles;
import com.shop.entity.MerchantGoods;
import com.shop.model.PageParam;
import com.shop.model.vo.Rsp;
import com.shop.service.MerchantGoodsService;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * (MerchantGoods)表控制层
 *
 * @author makejava
 * @since 2020-11-21 09:48:34
 */
@Validated
@RestController
@AllArgsConstructor
@RequestMapping("goods")
public class MerchantGoodsController {
    /**
     * 服务对象
     */
    private final MerchantGoodsService merchantGoodsService;

    @GetMapping("testApi")
    public String testApi(){
        throw new RuntimeException("这是个异常");//测试异常
    }

    /**
     * 查询商品列表
     */
    @RequireRoles(roles = {"merchant"})
    @PostMapping("getGoodsList")
    public Rsp getGoodsList(@RequestBody PageParam<MerchantGoods> pageParam,
                            @RequestHeader Integer merchantId){
        System.out.println(pageParam.toString()+"===========");
        return Rsp.success(merchantGoodsService.getGoodsList(pageParam,merchantId));
    }

    /**
     * 修改商品
     */
    @PutMapping("updateGoods")
    public Rsp updateGoods(@RequestBody MerchantGoods merchantGoods){
        return Rsp.success(merchantGoodsService.updateGoods(merchantGoods));
    }

    /**
     * 新增商品
     */
    @PostMapping("insertGoods")
    public Rsp insertGoods(@Valid @RequestBody MerchantGoods merchantGoods, @RequestHeader Integer merchantId){
        merchantGoods.setMerchantId(merchantId);//商家id
        return Rsp.success(merchantGoodsService.insertGoods(merchantGoods));
    }

    /**
     * 删除商品
     *
     */
    @PutMapping("delGoods")
    public Rsp delGoods(@RequestBody MerchantGoods merchantGoods){
        return Rsp.success(merchantGoodsService.delGoods(merchantGoods.getId()));
    }

    /**
     * 根据类别查商品
     */
    @PostMapping("getCategoryIdByMerchant")
    public Rsp getCategoryIdByMerchant(@RequestBody Integer categoryId,@RequestHeader Integer merchantId) {
        return  Rsp.success(merchantGoodsService.getCategoryIdByMerchant(categoryId,merchantId));
    }


    /**
     * 过商品类别id查询该商户下所有商品
     * @param CategoryId
     * @return
     */
    @GetMapping("getGoodsListByCategoryId")
    public Rsp getGoodsListByCategoryId(String CategoryId)
    {
        System.out.println(CategoryId);
        return new Rsp(200,"",merchantGoodsService.getGoodsListByCategoryId(CategoryId));
    }

}
